package com.tarena.lbs.activity.service.impl;

import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    ActivityRepository activityRepository;


    /*
     * 查询列表
     * */
    @Override
    public PageResult<ActivityVO> selectActivityList(ActivityQuery query) {
        int offset = (query.getPageNo() -1) * query.getPageSize();

        //查询当前页数据
        List<ActivityVO> list = activityRepository.selectListByQuery(query,offset);


        //查询总数
        int total = activityRepository.countByQuery(query);

        PageResult<ActivityVO> result = new PageResult<>();
        result.setObjects(list);
        result.setTotal((long) total);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());

        return result;
    }

    /*
     * 添加活动
     * */
 /*
  @Override
    public void saveActivity(ActivityAddParam param)  {
        ActivityPO activity = new ActivityPO();
        BeanUtils.copyProperties(activity,param);

        activity.setCreateAt(new Date()); // 创建时间
        activity.setUpdateAt(new Date());

       int rows =  activityRepository.insert(activity);
       if(rows<=0){
           throw new RuntimeException("活动添加失败");
       }

    }
    */


    /*
    * 查询详情
    * */
    @Override
    public ActivityVO getActivityDetailById(Integer id) throws BusinessException {

        ActivityVO activityVO = activityRepository.findById(id);
        
        if(activityVO==null){
            throw new RuntimeException("活动不存在");
        }

        return activityVO ;
    }

}
